﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardInChina.Data.Entity.Entity;

namespace CardInChina.Data.Entity.Interface
{
    public class GameService
    {
        public IEnumerable<Game> GetGamesByIndex(string index)
        {
            if (string.IsNullOrEmpty(index)) return null;
            using (CICEntities db = new CICEntities())
            {
                return db.Games
                    .Where(p => p.IsShow == 1 && p.IndexString == index)
                    .OrderBy(p => p.GameName.Length)
                    .ToArray();
            }
        }

        public IEnumerable<Game> GetHotGames()
        {
            using (CICEntities db = new CICEntities())
            {
                return db.Games
                    .Where(p => p.IsShow == 1 && p.IsHot == 1)
                    .OrderBy(p => p.IndexString)
                    .ToArray();
            }
        }

        public IEnumerable<Game> GetOtherGames()
        {
            using (CICEntities db = new CICEntities())
            {
                return db.Games
                    .Where(p => p.IsShow == 0)
                    .OrderBy(p => p.IndexString)
                    .ToArray();
            }
        }

        public IEnumerable<Game> GetHotTopGames(int top)
        {
            using (CICEntities db = new CICEntities())
            {
                return db.Games
                    .Where(p => p.IsHotTop == 1 && p.IsShow == 1)
                    .OrderBy(p => p.GameName)
                    .Take(top)
                    .ToArray();
            }
        }

        public IEnumerable<Game> GetGamesByKeyword(string Keyword)
        {
            using (CICEntities db = new CICEntities())
            {
                string[] keys = Keyword.Split(' ');
                foreach (var t in db.Games.OrderBy(p => p.GameName).ToArray())
                {
                    foreach (var key in keys)
                    {
                        if (t.GameName.Contains(key)){
                            yield return t;
                            break;
                        }
                    }
                }
            }
        }

        public Game GetGameByKey(string key)
        {
            using (CICEntities db = new CICEntities())
            {
                return db.Games.FirstOrDefault(p => p.GameKey == key);
            }
        }
    }
}
